-- ReprotView 举报功能UI

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.ReportScene

local ReportDescMaxWordNum = tonumber( GD:getSystemPar("reportDescMaxWordNum", 50) ) -- 举报输入最大字符数

function ClassRef:init()
    -- sureBtn
    self._ui.btn_ok:addClickEventListener(function()
        if self._selectBtnIndex == 6 then
            -- 选择了其他是否有输入理由
            if self._editBox:getText() == "" then
                display.pushToast(L("report_other_desc"))
                return
            end
        end
        -- 是否选择举报同时加入黑名单
        if self._selectAddBlack then
            app:sendMsg("FriendModel", "reqAddBlacklist", self._data.uid)
        end
        local info = {}
        info.uid = self._data.uid
        info.evilType = Enums.ReportEvilType[self._selectBtnIndex]
        info.desc = self._editBox:getText()
        executeFunc(self._onOperate, "reqSecReport", info)
    end)
    -- cancelBtn
    self._ui.btn_quxiao:addClickEventListener(function()
        executeFunc(self._onCloseView)
    end)
    -- selectBtn（单选）
    self._selectBtn = {}
    self._selectBtnIndex = 1
    for i = 1, 6 do
        local btn = self._ui["Panel/Panel_0/Panel_tick_"..i]
        table.insert(self._selectBtn, btn)
        if self._selectBtnIndex == i then
            btn:getChildByName("selected_btn"):setBright(false)
        end
    end
    for k, btn in ipairs(self._selectBtn) do
        btn:addClickEventListener(function()
            if self._selectBtnIndex ~= k then
                if self._selectBtn[self._selectBtnIndex] then
                    self._selectBtn[self._selectBtnIndex]:getChildByName("selected_btn"):setBright(true)
                end
                btn:getChildByName("selected_btn"):setBright(false)
                self._selectBtnIndex = k
            end
        end)    
    end
    -- blackBtn
    self._addBlackBtn = self._ui["Panel/Panel_0/Panel_tick_7"]
    self._addBlackBtn:addClickEventListener(function()
        local b = self._addBlackBtn:getChildByName("selected_btn"):isBright()
        self:setAddBlackBtnState(not b)
    end)
    -- TextField
    self:_initEditBox()
end

function ClassRef:refresh(data)
    self._data = data
    if self._data then
        local name = self._data.name
        self._ui.tips:setString(string.format( L("reportTips"), name ))
    end
end

function ClassRef:_initEditBox()
    local function onEditCallback(strEventName,sender)
		local edit = sender
		if strEventName == "changed" then
			local text = edit:getText()
			local ulen = string.counting(text)
			if ulen > ReportDescMaxWordNum then
				local newStr = string.usubstr(text,ReportDescMaxWordNum)
				if text~=newStr then
					edit:setText(newStr)
					text = newStr
				end
			end
		end
	end
    local textField = self._ui["Panel/Panel_0/TextField"]
    self._editBox = display.replaceTextFieldWithEditBox(textField, cc.EDITBOX_INPUT_MODE_ANY)
    self._editBox:registerScriptEditBoxHandler( onEditCallback )
end

function ClassRef:setAddBlackBtnState(b)
    self._selectAddBlack = not b
    self._addBlackBtn:getChildByName("selected_btn"):setBright(b)
end

return ClassRef